<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="账号">
              <a-input v-model="queryParam.account" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.real_name" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="手机号">
              <a-input v-model="queryParam.phone" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="角色">
              <a-select v-model="queryParam.role" placeholder="请选择">
                <a-select-option v-for="role in roles" :key="role.id" :value="role.name">{{ role.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="状态">
              <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                <a-select-option value="">全部</a-select-option>
                <a-select-option value="1">启用</a-select-option>
                <a-select-option value="2">禁用</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="创建时间">
              <a-range-picker :allowClear="true" ref="createdAtRangePicker" @change="createdAtRangePickerOnChange" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="() => {this.$router.push('/system/account_management/create')}">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span slot="action" slot-scope="text, record">
        <template>
<!--          <a @click="handleEdit(record)">编辑</a>-->
<!--          <a-divider type="vertical" />-->

          <a-popconfirm placement="top" @confirm="confirmStatusChange(record)">
            <template slot="title">
               确定要{{ record.status === 1 ? '禁用' : '启用' }}吗?
            </template>
            <a>{{ record.status === 1 ? '禁用' : '启用' }}</a>
          </a-popconfirm>
        </template>
      </span>
    </s-table>
  </a-card>
</template>

<script>
  import moment from 'moment'
  import { Ellipsis, STable } from '@/components'
  import { getManagerList, getRoleList, saveManager } from '@/api/system'

  const statusMap = {
    1: {
      text: '启用'
    },
    2: {
      text: '禁用'
    }
  }

export default {
  name: 'List',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      mdl: {},
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '账号',
          dataIndex: 'account'
        },
        {
          title: '管理员姓名',
          dataIndex: 'real_name'
        },
        {
          title: '手机号',
          dataIndex: 'phone'
        },
        {
          title: '角色',
          dataIndex: 'roles',
          customRender: roles => { return roles.map(item => { return item.name }).join(';') }
        },
        {
          title: '创建时间',
          dataIndex: 'created_at'
        },
        {
          title: '状态',
          dataIndex: 'status',
          customRender: status => { return statusMap[status].text }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getManagerList(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      roles: {}
    }
  },
  created () {
    getRoleList({ per_page: 999, status: 1, select: 'id,name', user_statistics: 0 }).then(res => {
      this.roles = res.data
    })
  },
  methods: {
    createdAtRangePickerOnChange (date, dateString) {
      this.queryParam.created_at_begin = dateString[0]
      this.queryParam.created_at_end = dateString[1]
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        date: moment(new Date())
      }
    },
    handleReset () {
      this.queryParam = {}
      this.$refs.createdAtRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
    },
    confirmStatusChange (record) {
      let status = 1
      if (record.status === 1) {
        status = 2
      }
      saveManager({ id: record.id, status: status }).then(() => this.$refs.table.refresh())
    }
  }
}
</script>
